#!/bin/sh
#
# $NetBSD$
#

# PROVIDE: mfsvlog
# REQUIRE: mountcritremote 
# BEFORE:  SERVERS syslogd 
# KEYWORD: shutdown

$_rc_subr_loaded . /etc/rc.subr

name="mfsvlog"
rcvar=$name
start_cmd="mfsvlog_start"
stop_cmd="mfsvlog_stop"

# defaults override in rc.conf
mfsvlog_default_logfiles="messages authlog maillog cron" 
mfsvlog_logfiles=${mfsvlog_logfiles:=$mfsvlog_default_logfiles}
mfsvlog_archive=YES
mfsvlog_archivedir="/shadow/var/logarchive"

mfsvlog_start()
{
	
	echo -n "Creating temporary logfiles in MFS: "
	for logfile in $mfsvlog_logfiles; do 
		umask 066
		echo -n "${logfile} "
		touch /var/log/${logfile}
	done

}

mfsvlog_stop()
{

	if checkyesno mfsvlog_archive; then
		datecode=$(date +"%m%d%Y-%H%M%S")
		echo -n "Archiving active log files to CF media:"
		if [ -d ${mfsvlog_archivedir} -a -w ${mfsvlog_archivedir} ]; then
			mkdir -p ${mfsvlog_archivedir}/${datecode}
			for logfile in $mfsvlog_logfiles; do
				umask 066
				echo -n "${logfile} "
				cp -rp /var/log/${logfile}* ${mfsvlog_archivedir}/${datecode}
			done
		else
			echo "Unable to write to archive directory: $mfsvlog_archivedir"
		fi
				

        fi

}

load_rc_config $name
run_rc_command "$1"
